/**
 * Created with IntelliJ IDEA.
 * User: tsegelskyi
 * Date: 15.02.13
 * Time: 15:01
 * To change this template use File | Settings | File Templates.
 */
public class UFTest {
    public static void main(String[] args) {
        int N = StdIn.readInt();
        UF_QuickFind uf = new UF_QuickFind(N);
        while (!StdIn.isEmpty()){
            int p = StdIn.readInt();
            int q = StdIn.readInt();
            if (!uf.connected(p,q)){
                uf.union(p,q);
                StdOut.println(p + " " + q);
            }
        }
    }

}
